﻿using AutoMapper;
using Sustainalytics.SharedEntities.EsgResearch;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Sustainalytics.ClientDW.Synchronization.SyncESGData.RPCompanyESGComputationResultLogic
{
    public class RPCompanyESGComputationResultMapper
    {
        public ESG.Entities.RPCompanyESGComputationResult Map(EsgResearchProfile entity)
        {
            var computationResult = Mapper.Map<ESG.Entities.RPCompanyESGComputationResult>(entity.ComputationResult);

            var extendedIndicators = entity.Indicators.ToDictionary(k => k.Number);

            var indicators = entity.ComputationResult.Indicators.Select(Mapper.Map<ESG.Entities.RPIndicatorPerformance>).ToList();
            

            Parallel.ForEach(indicators, indicator =>
            {
                if (extendedIndicators.ContainsKey(indicator.Code))
                {
                    indicator.SelectedAnswer = Mapper.Map<Entities.AnswerCategory>(extendedIndicators[indicator.Code].SelectedAnswer);
                }
            });

            computationResult.Indicators = indicators.ToArray();
            computationResult.Id = entity.Id;

            return computationResult;
        }

        public IList<ESG.Entities.RPCompanyESGComputationResult> MapManyToMany(IList<EsgResearchProfile> entities)
        {
            return entities.Select(Map).ToList();
        }

    }
}
